package com.gzc.just.play.last.war.sceneserver.battle.model;

import jakarta.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 战斗信息（对应C++的SSBattle）
 * 包含战斗的核心数据和状态
 */
@Entity
@Table(name = "battle_info")
public class BattleInfo implements Serializable {
    private static final long serialVersionUID = 1L;

    // ==================== 战斗基础信息 ====================
    /** 战斗ID */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "battle_id")
    private Long battleId;
    
    /** 战斗状态 */
    @Enumerated(EnumType.ORDINAL)
    private BattleStatus status;
    
    /** 战斗类型 */
    @Enumerated(EnumType.ORDINAL)
    private BattleType battleType;
    
    // ==================== 战斗时间 ====================
    /** 战斗开始时间 */
    private Long startTime;
    
    /** 战斗结束时间 */
    private Long endTime;
    
    /** 战斗持续时间（毫秒） */
    private Long duration;
    
    // ==================== 参战玩家 ====================
    /** 战斗中的玩家信息列表 */
    @OneToMany(mappedBy = "battleInfo", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<BattleUserInfo> battleUsers = new ArrayList<>();
    
    /** 队伍1玩家列表 */
    private List<Long> team1Players = new ArrayList<>();
    
    /** 队伍2玩家列表 */
    private List<Long> team2Players = new ArrayList<>();
    
    // ==================== 战场信息 ====================
    /** 地图ID */
    private Integer mapId;
    
    /** 最大玩家数 */
    private Integer maxPlayers;
    
    // ==================== 战斗结果 ====================
    /** 赢家队伍ID (1 或 2) */
    private Integer winnerTeamId;
    
    /** 赢家列表 */
    private List<Long> winnerList = new ArrayList<>();
    
    /** 战斗记录 */
    private BattleRecord battleRecord;
    
    // ==================== 其他信息 ====================
    /** 是否已保存 */
    private Boolean saved;
    
    /** 心跳时间 */
    private Long heartBeatTime;
    
    /**
     * 添加玩家到战斗
     */
    public void addBattleUser(BattleUserInfo userInfo) {
        if (battleUsers == null) {
            battleUsers = new ArrayList<>();
        }
        battleUsers.add(userInfo);
    }
    
    /**
     * 获取战斗中的玩家数
     */
    public Integer getPlayerCount() {
        return battleUsers != null ? battleUsers.size() : 0;
    }
    
    /**
     * 检查战斗是否已满
     */
    public boolean isFull() {
        return getPlayerCount() >= maxPlayers;
    }
    
    /**
     * 检查战斗是否进行中
     */
    public boolean isInProgress() {
        return status == BattleStatus.IN_PROGRESS;
    }
    
    // ==================== Getter和Setter方法 ====================
    
    public Long getBattleId() {
        return battleId;
    }
    
    public void setBattleId(Long battleId) {
        this.battleId = battleId;
    }
    
    public BattleStatus getStatus() {
        return status;
    }
    
    public void setStatus(BattleStatus status) {
        this.status = status;
    }
    
    public BattleType getBattleType() {
        return battleType;
    }
    
    public void setBattleType(BattleType battleType) {
        this.battleType = battleType;
    }
    
    public Long getStartTime() {
        return startTime;
    }
    
    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }
    
    public Long getEndTime() {
        return endTime;
    }
    
    public void setEndTime(Long endTime) {
        this.endTime = endTime;
    }
    
    public Long getDuration() {
        return duration;
    }
    
    public void setDuration(Long duration) {
        this.duration = duration;
    }
    
    public List<BattleUserInfo> getBattleUsers() {
        return battleUsers;
    }
    
    public void setBattleUsers(List<BattleUserInfo> battleUsers) {
        this.battleUsers = battleUsers;
    }
    
    public List<Long> getTeam1Players() {
        return team1Players;
    }
    
    public void setTeam1Players(List<Long> team1Players) {
        this.team1Players = team1Players;
    }
    
    public List<Long> getTeam2Players() {
        return team2Players;
    }
    
    public void setTeam2Players(List<Long> team2Players) {
        this.team2Players = team2Players;
    }
    
    public Integer getMapId() {
        return mapId;
    }
    
    public void setMapId(Integer mapId) {
        this.mapId = mapId;
    }
    
    public Integer getMaxPlayers() {
        return maxPlayers;
    }
    
    public void setMaxPlayers(Integer maxPlayers) {
        this.maxPlayers = maxPlayers;
    }
    
    public Integer getWinnerTeamId() {
        return winnerTeamId;
    }
    
    public void setWinnerTeamId(Integer winnerTeamId) {
        this.winnerTeamId = winnerTeamId;
    }
    
    public List<Long> getWinnerList() {
        return winnerList;
    }
    
    public void setWinnerList(List<Long> winnerList) {
        this.winnerList = winnerList;
    }
    
    public BattleRecord getBattleRecord() {
        return battleRecord;
    }
    
    public void setBattleRecord(BattleRecord battleRecord) {
        this.battleRecord = battleRecord;
    }
    
    public Boolean getSaved() {
        return saved;
    }
    
    public void setSaved(Boolean saved) {
        this.saved = saved;
    }
    
    public Long getHeartBeatTime() {
        return heartBeatTime;
    }
    
    public void setHeartBeatTime(Long heartBeatTime) {
        this.heartBeatTime = heartBeatTime;
    }
    
    /**
     * 战斗状态枚举
     */
    public enum BattleStatus {
        WAITING("等待中"),
        LOADING("加载中"),
        IN_PROGRESS("进行中"),
        FINISHED("已结束"),
        CANCELLED("已取消");
        
        private final String description;
        
        BattleStatus(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    /**
     * 战斗类型枚举
     */
    public enum BattleType {
        PVP("玩家对玩家"),
        PVE("玩家对环境"),
        GUILD("公会战"),
        ARENA("竞技场"),
        TOURNAMENT("锦标赛");
        
        private final String description;
        
        BattleType(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
}
